package com.nj9you.sdkdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.nj9you.sdk.Nj9youSdk;
import com.nj9you.sdk.framework.PayResult;
import com.nj9you.sdk.listener.OnUserExchangeListener;
import com.nj9you.sdk.listener.OnUserLoginListener;
import com.nj9you.sdk.listener.OnUserLogoutListener;
import com.nj9you.sdk.listener.PayCallback;
import com.nj9you.sdk.params.PayParams;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class MainActivity extends Activity {

    private static final String TAG = "sdkdemo";

    public static String generateOrderFor20Bit() {
        SimpleDateFormat sdf_yyyyMMddHHmmss = new SimpleDateFormat("yyyyMMddHHmmss");
        Random RANDOM = new Random();
        StringBuilder sb = new StringBuilder(20);
        sb.append(sdf_yyyyMMddHHmmss.format(new Date()));
        sb.append(100000 + RANDOM.nextInt(900000));
        return sb.toString();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        if (view.getId() == R.id.init) {
            Nj9youSdk.init(this, "2");
            Toast.makeText(this, "初始化完成", Toast.LENGTH_SHORT).show();
        } else if (view.getId() == R.id.login_test) {
            login();
        } else if (view.getId() == R.id.function_test) {
        } else if (view.getId() == R.id.exchange) {
            Nj9youSdk.exchange(this, new OnUserExchangeListener() {
                @Override
                public void onExchangeComplete(boolean success, String loginInfo) {
                    Toast.makeText(MainActivity.this, "切换账号：" + loginInfo, Toast.LENGTH_SHORT).show();

                }
            });
        } else if (view.getId() == R.id.pay) {
            PayParams payParams = new PayParams();
            payParams.setSubject("幸运捕鱼-购买80金币(赠送20金币)");
            payParams.setPrice("1");
            payParams.setAttach("njjy_alipay");
            payParams.setOrder(generateOrderFor20Bit());
            payParams.setBody("购买炮弹");
            payParams.setServer("万箭齐发一区");
            payParams.setItemid(generateOrderFor20Bit());
            Nj9youSdk.pay(this, payParams, new PayCallback() {
                @Override
                public void onPayResult(int code, String desc, PayParams payParams) {
                    Log.d(TAG, "code : " + code + " , desc : " + desc + " , payParams : " + payParams);
                    showPayResult(code, desc, payParams);
                }
            });
        } else if (view.getId() == R.id.logout) {
            Nj9youSdk.logout(this, new OnUserLogoutListener() {
                @Override
                public void onLogoutComplete(boolean success, String username) {
                    Toast.makeText(MainActivity.this, success ? "注销成功 : " + username : "注销失败", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    private void login() {
        Nj9youSdk.login(this, new OnUserLoginListener() {
            @Override
            public void onLoginComplete(boolean success, String loginInfo) {
                Toast.makeText(MainActivity.this, "登录账号：" + loginInfo, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void showPayResult(int error, String desc, PayParams payParams) {
        String title = "支付成功";
        if (error == PayResult.PAY_SUCCESS) {
            title = "支付成功";
        } else if (error == PayResult.PAY_FAILURE) {
            title = "支付失败";
        } else if (error == PayResult.PAY_CANCEL) {
            title = "支付取消";
        } else if (error == PayResult.PAY_WAITING) {
            title = "支付等待中";
        } else if (error == PayResult.PAY_ONGOING) {
            title = "支付正在进行中";
        }
        title += " : " + error;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(desc + "\n" + payParams);
        builder.setPositiveButton(android.R.string.ok, null);
        builder.create().show();
    }

    private void showLoginResult(String title, String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(title);
        builder.setMessage(msg);
        builder.setPositiveButton(android.R.string.ok, null);
        builder.create().show();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (false) {
            Nj9youSdk.exit(this, new Nj9youSdk.OnExitCallback() {
                @Override
                public void onExitConfirm() {
                    finish();
                }

                @Override
                public void onExitCancel() {
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Nj9youSdk.exit(this);
    }
}
